<?php
namespace Api;
use Kuyuan\WqCore\api\Api;
use Kuyuan\WqCore\util\Util;

/**
 * 订阅消息
 * Class Link
 * @package Api
 */
class SubMessage extends Api
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 检测是否显示订阅消息弹窗，并且返回模板消息id
     */
    public function testShowSubMessage()
    {
        Util::appointGet();
        $_returns = array();
        // 订阅类型 media=素材库审核通过 coupon=卡券审核通过
        $_type = $this->_gpc["type"];
        try {
            $WeChat = $this->newController("WeChat");
            // 是否关注，如果关注了不提示订阅直接发模板消息
            if($WeChat->testAttention()) {
                throw new \Exception("已关注公众号",1000);
            }
            // 系统是否开启了
            if($_type == 'media') {
                $SettingMedia = $this->newController("SettingMedia");
                $_setting_media = $SettingMedia->find();
                if(!$_setting_media["is_check"]) {
                    throw new \Exception("未开启素材审核",1000);
                }
            } else if($_type == 'coupon') {
                $Setting = $this->newController("Setting");
                $_setting = $Setting->find();
                if(!$_setting["coupon_check"]) {
                    throw new \Exception("未开启卡券审核",1000);
                }
            }
            $SettingMessage = $this->newController("SettingMessage");
            $_setting_message = $SettingMessage->find();
            if(empty($_setting_message["result_check_sub"])) {
                throw new \Exception("未设置模板id",1000);
            }
            $_returns = array("code"=>0,"data"=>$_setting_message["result_check_sub"]);
        }catch (\Exception $e) {
            $_returns = array("code"=>$e->getCode(),"err_msg"=>$e->getMessage());
        }
        Util::returns($_returns);
    }

}